package org.utils
{
	import flash.display.Sprite;
	import flash.filesystem.File;

	/**
	 * 数组扩展类，增加乱序和去重功能 
	 * @author Administrator
	 * 
	 */    
	public class ArrayControl extends Array
	{
		public function ArrayControl()
		{
		}

		/**
		 * 打乱顺序 
		 * @param value 数组对象
		 * @return Array
		 * 
		 */		
		public static function confusion(value:Array):Array
		{
			var len:Number=value.length
			for (var i:Number=0; i < len; i++)
			{
				var num:Number=Math.floor(Math.random() * len)
				var obj:Array=value.splice(num, 1)
				value.unshift(obj[0])
			}
			return value;
		}
		;

		/**
		 * 去重重复元素
		 * @param value 数组对象
		 * @param sort true|false 从后往前排除|从前往后排除
		 * @return Array
		 * 
		 */		
		public static function defoam(value:Array,sort:Boolean=false):Array
		{
			var arrCopy:Array=[]
				
			if(sort){
			   
				value=value.reverse()
				
			}
				
			while (value.length > 0)
			{
				var obj:*=value.shift()
				for (var i:Number=0; i < value.length; i++)
				{
					var symbol:*=value[i]
					if (obj === symbol || obj == symbol)
					{
						value.splice(i, 1)
						i--
					}
				}
				arrCopy.push(obj)
			}
			return arrCopy;
		}
		
		
		
		/**
		 * 排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderX(arr:Array):Array{	
			
		function sortOnX(a, b):Number {
			var aPrice:Number = a.x;
			var bPrice:Number = b.x;
			
			if(aPrice > bPrice) {
				return 1;
			} else if(aPrice < bPrice) {
				return -1;
			} else  {
				//aPrice == bPrice
				return 0;
			}
		}
		
		return arr.sort(sortOnX)	
		
		}

		/**
		 * 排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderY(arr:Array):Array{	
			
			function sortOnY(a, b):Number {
				var aPrice:Number = a.y;
				var bPrice:Number = b.y;
				
				if(aPrice > bPrice) {
					return 1;
				} else if(aPrice < bPrice) {
					return -1;
				} else  {
					//aPrice == bPrice
					return 0;
				}
			}
			
			return arr.sort(sortOnY)	
			
		}
		
		
		/**
		 * 按大小排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderDate(arr:Array):Array{	
			
			function sortOnY(a:File, b:File):Number {
				var aPrice:Number = a.creationDate.valueOf();
				var bPrice:Number = b.creationDate.valueOf();
				
				if(aPrice > bPrice) {
					return 1;
				} else if(aPrice < bPrice) {
					return -1;
				} else  {
					//aPrice == bPrice
					return 0;
				}
			}
			
			return arr.sort(sortOnY)	
			
		}
		
		
		/**
		 * 按深度排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderDepth(arr:Array):Array{	
			
			function sortOnY(a:Sprite, b:Sprite):Number {
				var aPrice:Number = a.parent.getChildIndex(a)
				var bPrice:Number = b.parent.getChildIndex(b)
				
				if(aPrice > bPrice) {
					return 1;
				} else if(aPrice < bPrice) {
					return -1;
				} else  {
					//aPrice == bPrice
					return 0;
				}
			}
			
			return arr.sort(sortOnY)	
			
		}
		
		/**
		 * 按包含关系排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderRoot(arr:Array):Array{	

			//筛选出空的
			var arrcopy:Array=[]
			for(var ii=0;ii<arr.length;ii++){
			
			    if(arr[ii].iv==""){
					arrcopy.push(arr[ii])
					arr.splice(ii,1)
					ii--
				}
				
			}
			
			for(var i=0;i<arr.length;i++){

				for(var j=0;j<arr.length;j++){
					
					if(arr[i].iv.indexOf(arr[j].ivname)!=-1){
					   
						if(j<i){
					    var rememberA = arr[i]
						var rememberB = arr[j]
						arr[j] =  rememberA
						arr[i] =  rememberB	 
						i--
                        j=0
						}
						
					}
				
				}
				
			}
			return arrcopy.concat(arr.reverse())

		}
		
		
		/**
		 * 按包含关系排序
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */
		public static function orderXML(arr:Array):Array{	
			
			//筛选出空的
			var arrcopy:Array=[]
			for(var ii=0;ii<arr.length;ii++){
				
				if(arr[ii].sub==""){
					arrcopy.push(arr[ii])
					arr.splice(ii,1)
					ii--
				}
				
			}
			
			for(var i=0;i<arr.length;i++){
				
				for(var j=0;j<arr.length;j++){
					
					if(arr[i].sub.indexOf(arr[j].dopath)!=-1){
						
						if(j<i){
							var rememberA = arr[i]
							var rememberB = arr[j]
							arr[j] =  rememberA
							arr[i] =  rememberB	 
							i--
							j=0
						}
						
					}
					
				}
				
			}
			return arrcopy.concat(arr.reverse())
			
		}
		
		
		
	}
}
